/**
 * @author ahammer
 * @date   2009
 * @update 2010 11 modified by Philipp W. <java@scaenicus.net> in order to save user preferences (xml templates)
 * @update 2010 11 JScrollPane for top level JFrame
 * 
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package opgui;

import java.awt.event.ActionEvent;
import javax.media.jai.PlanarImage;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JCheckBox;
import op.IqmOperator;
import main.IqmTools;
import main.Look;
import main.Manager;
import main.Tank;
import java.awt.GridBagConstraints;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.media.jai.ParameterBlockJAI;
import javax.swing.JComboBox;

import opgui.preferences.JAIXMLParameterBlocks;
import opgui.preferences.TemplateManagerGUI;
import opgui.preferences.jaixml.JaiOperationDescriptor;
import opgui.preferences.jaixml.JaiPreferences.ParameterBlock;

/**
 * This class is the basis for every operation GUI.
 
 */
public abstract class IqmOperatorJFrame extends JFrame implements WindowListener {

    private static final long serialVersionUID = 1L;
    private JScrollPane jScrollPane  = null;
    private JPanel      jContentPane = null;
    private static JCheckBox jCheckBoxAutoPreview = null;
    private static JCheckBox jCheckBoxInvert = null;
    private JButton jButtonPreview = null;
    private JButton jButtonReset = null;
    private JButton jButtonQuit = null;
    private javax.swing.JComboBox chooseParameterBlock = null;
//    private javax.swing.JTextField newTemplateName = null;
    private List<JaiOperationDescriptor> jaiTemplates;
    private javax.swing.DefaultComboBoxModel templateList = null;
    private TemplateManagerGUI templateManager = null;
    private int lastChoosenTemplateIndex = 0;

    /**
     * This is the default constructor
     */
    public IqmOperatorJFrame() {
        //super();
        //initialize();
    }

    /**
     * This method initializes this
     *
     * @return void
     */
    public void initialize() {
        this.setLocation(1, 80);
        //this.setSize(320, 200);
        this.setContentPane(getJScrollPane());
        this.setTitle("JFrame");
        //this.setIconImage(new ImageIcon("imgs/IqmLogoBlue32.gif").getImage());
        URL url = ClassLoader.getSystemResource("imgs/IqmLogoOrange32_v5.png");
        if (url != null) {
            this.setIconImage(new ImageIcon(url).getImage());
        }
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);  //eigene close routine
        this.addWindowListener(this);
        this.pack();
        this.setVisible(true);
        //IqmTools.setCurrIqmOperatorJFrame(this);
    }

    //----------------------------------------------------------------------------------------
    public void destroyIqmOperatorGUI() {
        //This may be overwritten by the subclass
        if (this != null) {
            this.setVisible(false);
            this.dispose();
        }
    }

    //----------------------------------------------------------------------------------------
    /**
     * This updates the GUI of a subclass
     * If it is needed it must be implemented in subclass
     */
    public void updateIqmOperatorGUI() {
        // DOES NOTHING
        //Must be overwritten in subclass if it should do something
        //e.g. IqmThreshold: Histogram update
    }
    //----------------------------------------------------------------------------------------

    /**
     * This method initializes jScrollPane
     * This is the top ScrollPane for the GUI's
     * @return javax.swing.JScrollPane
     */
    public JScrollPane getJScrollPane() {
    	if (jScrollPane == null){
    		jScrollPane = new JScrollPane(getJContentPane());
    	}
    	return jScrollPane;
    }
    //----------------------------------------------------------------------------------------
    /**
     * This method initializes jContentPane
     *
     * @return javax.swing.JPanel
     */
     public JPanel getJContentPane() {
    	 if (jContentPane == null) {
            GridBagConstraints gridBagConstraintsAutoPreview = new GridBagConstraints();
            gridBagConstraintsAutoPreview.gridx = 1;
            gridBagConstraintsAutoPreview.gridy = 1;
            gridBagConstraintsAutoPreview.insets = new Insets(15, 10, 0, 10);

            GridBagConstraints gridBagConstraintsInvert = new GridBagConstraints();
            gridBagConstraintsInvert.gridx = 2;
            gridBagConstraintsInvert.gridy = 1;
            gridBagConstraintsInvert.gridwidth = 2;
            gridBagConstraintsInvert.insets = new Insets(15, 10, 0, 10);


            GridBagConstraints gridBagConstraintsPreview = new GridBagConstraints();
            gridBagConstraintsPreview.gridx = 1;
            gridBagConstraintsPreview.gridy = 2;
            gridBagConstraintsPreview.ipadx = 20; //Breite
            gridBagConstraintsPreview.insets = new Insets(10, 10, 10, 5);


            GridBagConstraints gridBagConstraintsReset = new GridBagConstraints();
            gridBagConstraintsReset.gridx = 2;
            gridBagConstraintsReset.gridy = 2;
            gridBagConstraintsReset.ipadx = 20; //Breite
            gridBagConstraintsReset.insets = new Insets(10, 5, 10, 5);


            GridBagConstraints gridBagConstraintsQuit = new GridBagConstraints();
            gridBagConstraintsQuit.gridx = 3;
            gridBagConstraintsQuit.gridy = 2;
            gridBagConstraintsQuit.anchor = GridBagConstraints.LINE_START;
            gridBagConstraintsQuit.ipadx = 20; //Breite
            gridBagConstraintsQuit.insets = new Insets(10, 5, 10, 5);
                        
            GridBagConstraints gridBagConstraintsTemplateLabel = new GridBagConstraints();
            gridBagConstraintsTemplateLabel.gridx = 1;
            gridBagConstraintsTemplateLabel.gridy = 3;
            gridBagConstraintsTemplateLabel.gridwidth = 1;
            //gridBagConstraintsTemplateLabel.anchor = GridBagConstraints.LINE_START;
            //gridBagConstraintsTemplateLabel.ipadx = 20; //Breite
            gridBagConstraintsTemplateLabel.insets = new Insets(10, 5, 10, 10);

            GridBagConstraints gridBagConstraintsChooseParameterBlock = new GridBagConstraints();
            gridBagConstraintsChooseParameterBlock.gridx = 2;
            gridBagConstraintsChooseParameterBlock.gridy = 3;
            gridBagConstraintsChooseParameterBlock.gridwidth = 2;
            //gridBagConstraintsChooseParameterBlock.anchor = GridBagConstraints.LINE_START;
            //gridBagConstraintsChooseParameterBlock.ipadx = 20; //Breite
            gridBagConstraintsChooseParameterBlock.insets = new Insets(10, 5, 10, 10);

//            GridBagConstraints gridBagConstraintsNewTemplateName = new GridBagConstraints();
//            gridBagConstraintsNewTemplateName.gridx = 3;
//            gridBagConstraintsNewTemplateName.gridy = 3;
//            gridBagConstraintsNewTemplateName.anchor = GridBagConstraints.LINE_START;
//            gridBagConstraintsNewTemplateName.ipadx = 100; //Breite
//            gridBagConstraintsNewTemplateName.insets = new Insets(10, 5, 10, 10);


            GridBagLayout gridBagLayout = new GridBagLayout();
            //gridBagLayout.setRows(10);
            //gridBagLayout.setColumns(10);
            jContentPane = new JPanel();
            jContentPane.setLayout(gridBagLayout);
            jContentPane.add(getJCheckBoxAutoPreview(), gridBagConstraintsAutoPreview);
            jContentPane.add(getJCheckBoxInvert(),      gridBagConstraintsInvert);
            jContentPane.add(getJButtonPreview(),       gridBagConstraintsPreview);
            jContentPane.add(getJButtonReset(),         gridBagConstraintsReset);
            jContentPane.add(getJButtonQuit(),          gridBagConstraintsQuit);
//            jContentPane.add(getNewTemplateName(), gridBagConstraintsNewTemplateName);
            jContentPane.add(getTemplateLabel(),        gridBagConstraintsTemplateLabel);
            jContentPane.add(getChooseParameterBlock(), gridBagConstraintsChooseParameterBlock);
        }
        return jContentPane;
    }

    public JCheckBox getAutoPreview() {
        return jCheckBoxAutoPreview;
    }
    //----------------------------------------------------------------------------------------
    /**
     * This function sets the JAIParameterBlock in the GUI.<br />
     * Has to be overridden by the GUI-class.
     * @author Philipp W. <java@scaenicus.net>
     * @param pb ParameterBlockJAI to be set in the GUI.
     */
    public abstract void setParameterBlock(ParameterBlockJAI pb);

    /**
     * This recalls the setting to calculate dynamic values.<br />
     * Has to be overridden by the GUI-class.
     * @author Philipp W. <java@scaenicus.net>
     */
    public abstract void setParameterBlock();

    /**
     * This function sets the JAIParameterBlock in the GUI.<br />
     * Has to be overridden by the GUI-class.
     * @author Philipp W. <java@scaenicus.net>
     */
    public abstract void setParameterValues();

    /**
     * Is used to call the "parameter" event, when the template changes.
     * @param e setParametersInGUI creates a new ActionEvent with ID -1 and ActionCommand "parameter"
     * @see IqmOperatorJFrame#setParametersInGUI()
     */
    public abstract void actionPerformed(ActionEvent e);

    //----------------------------------------------------------------------------------------
    //methods for preference settings
    /**
     * This method is called to set the GUI and dynamic values.
     */
    private void setParametersInGUI() {
    	//System.out.println("IqmOperatorFrame: I'm here");
    	setParameterValues();
        setParameterBlock();  	
        //UNCOMMENT THIS IF YOU WANT TO IMPLEMENT THE DYNAMIC PARAMETERS FUNCTION
        //this.actionPerformed(new ActionEvent(this, -1, "parameter")); or better:    
        updateIqmOperatorGUI();
		//preview if selected
//		if (this.isAutoPreviewSelected()){
//			this.showPreview();
//		}
    }

    /**
     * Sets the GUIs ParameterBlock with a template saved in the jaitemplates.xml. <br />
     * This function is called, by the combo-box. If called otherwise make sure the template exists, as nothing will happen on a wrong name.
     * @author Philipp W. <java@scaenicus.net>
     * @param templatename Name of the template in jaitemplates.xml
     */
    private void chooseParameterBlockChoosen(String templatename) {
        //              //String currFunc = ("iqm" + this.getTitle()).toLowerCase();
        String currFunc = IqmTools.getCurrImgProcessFunc();
        JaiOperationDescriptor template = IqmTools.getJaiTemplatesXML().getTemplateByName(currFunc, templatename);
        if (template != null) {
            ParameterBlockJAI pb = new ParameterBlockJAI(currFunc.toLowerCase());
            pb = IqmTools.getJaiTemplatesXML().loadParameterBlockFromTemplate(pb, template);
            this.setParameterBlock(pb);
            if (this.isVisible()) { //Prevent calling before GUI is loaded, which leads to an error
                setParametersInGUI();
            }
        }
    }
    
    public void setChoosenTemplate(String templatename){
    	getTemplateList().setSelectedItem(templatename);	
    }

    private void handleChooseParameterBlockChange() {
        if (getChooseParameterBlock().isEditable()) { //Only if user might be the reason for the event.
            String choosen = (String) getChooseParameterBlock().getSelectedItem();
            if (getChooseParameterBlock().getSelectedIndex() == getChooseParameterBlock().getItemCount() - 1) {
                getChooseParameterBlock().setEditable(false);
                getTemplateManager().setSelectedItem(lastChoosenTemplateIndex);
                getTemplateManager().setVisible(true);
            } else {
                chooseParameterBlockChoosen(choosen);
                lastChoosenTemplateIndex = getChooseParameterBlock().getSelectedIndex();
            }
        }      
            
    }

    /**
     * This function sets the GUIs ParameterBlock to the user's choice.<br />
     * This function is called, when the changes the item in the combo-box.
     * @author Philipp W. <java@scaenicus.net>
     * @param evt Unused meta-data from the closing of the item changeing.
     */
    private void chooseParameterBlockItemStateChanged(java.awt.event.ItemEvent evt) {
       	//System.out.println("IqmOperatorFrame: I'm here: handleChooseParameterBlockChange()"); 	 
        handleChooseParameterBlockChange();
    }

    /**
     * This function sets the GUIs ParameterBlock to the user's choice.<br />
     * This function is called, when the user closes the combobox's popup menu.
     * @author Philipp W. <java@scaenicus.net>
     * @param evt Unused meta-data from the closing of the popup menu.
     */
    private void chooseParameterBlockPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
        //handleChooseParameterBlockChange();
    }

    public TemplateManagerGUI getTemplateManager() {
        if (templateManager == null) {
            final IqmOperatorJFrame callingGUI = this;
            templateManager = new TemplateManagerGUI(callingGUI);
            java.awt.EventQueue.invokeLater(new Runnable() {

                @Override
                public void run() {
                    templateManager.setVisible(true);
                }
            });

        }
        return templateManager;
    }

    /**
     * This function is called to fill the combo-box with appropriate templates from jaitemplates.xml.<br />
     * The entries are sorted alphabetically, but the first entry is "default" and the last is "New Template".<br />
     * If no template is found in jaitemplates.xml this function also generates a new "default" template from the hard-coded default values and saves them to jaitemplates.xml.
     * @author Philipp W. <java@scaenicus.net>
     */
    public void fillChooseParameterBlock() {
        getTemplateList().removeAllElements();
        getChooseParameterBlock().setEditable(false);
        String currFunc = IqmTools.getCurrImgProcessFunc();
        List<String> callstrings = new ArrayList<String>();
        boolean defaultisset = false;
    
        ParameterBlock xmlPB = IqmTools.getJaiTemplatesXML().getParameterBlockByName(currFunc);
        if (xmlPB != null) {
            jaiTemplates = xmlPB.getTemplate();
            for (JaiOperationDescriptor tlt : jaiTemplates) {
                if (tlt.getTemplatename().equalsIgnoreCase("default")) {
                    defaultisset = true;
                } else {
                    callstrings.add(tlt.getTemplatename());
                }
            }
        }
        java.util.Collections.sort(callstrings);
        callstrings.add(0, "default");
        //if (defaultisset == false) { //modified by ahammer: always load default ParameterBlockJAI parameters
            ParameterBlockJAI pb = new ParameterBlockJAI(currFunc.toLowerCase());
            IqmTools.getJaiTemplatesXML().setTemplate(currFunc, "default", pb);
        //}

        callstrings.add("<Start template manager>");

        for (String s : callstrings) {
            getTemplateList().addElement(s);
        }
        getChooseParameterBlock().setEditable(true);
        getChooseParameterBlock().validate();

        chooseParameterBlockChoosen("default");
        lastChoosenTemplateIndex = 0;
    }

    public DefaultComboBoxModel getTemplateList() {
        if (templateList == null) {
            templateList = new javax.swing.DefaultComboBoxModel();
            templateList.addElement("default");
            templateList.addElement("<New Template>");
        }
        return templateList;
    }
    private JLabel getTemplateLabel(){
    	JLabel jLabel = new JLabel("Preferences:");
    	return jLabel;
    }

    /**
     * Initializes and/or returns the combo-box used to choose existing templates (parameter block).
     * @author Philipp W. <java@scaenicus.net>
     * @return JComboBox Representation of the combo-box.
     */
    private JComboBox getChooseParameterBlock() {

        if (chooseParameterBlock == null) {
            chooseParameterBlock = new javax.swing.JComboBox();
            chooseParameterBlock.setPreferredSize(new Dimension(140,20));
            chooseParameterBlock.setModel(getTemplateList());

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

            fillChooseParameterBlock();

            chooseParameterBlock.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {

                @Override
                public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
                }

                @Override
                public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
                    //chooseParameterBlockPopupMenuWillBecomeInvisible(evt);
                }

                @Override
                public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
                }
            });

            chooseParameterBlock.addItemListener(new java.awt.event.ItemListener() {

                @Override
                public void itemStateChanged(java.awt.event.ItemEvent evt) {
                    chooseParameterBlockItemStateChanged(evt);
                }
            });
//            chooseParameterBlock.addKeyListener(new java.awt.event.KeyAdapter() {
//
//                @Override
//                public void keyPressed(java.awt.event.KeyEvent evt) {
//                    chooseParameterBlockKeyPressed(evt);
//                }
//            });


// <editor-fold defaultstate="collapsed" desc="NetBeans Layout (not used)">
/*javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(chooseParameterBlock, 0, 380, Short.MAX_VALUE)
            .addContainerGap())
            );
            layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(chooseParameterBlock, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(267, Short.MAX_VALUE))
            );*/// </editor-fold>
        }
        return chooseParameterBlock;
    }

    //----------------------------------------------------------------------------------------
    /**
     * This method initializes jCheckBoxAutoPreview
     *
     * @return javax.swing.JCheckBox
     */
    private JCheckBox getJCheckBoxAutoPreview() {
        if (jCheckBoxAutoPreview == null) {
            jCheckBoxAutoPreview = new JCheckBox();
            //jCheckBoxAutoPreview.setName("Auto Preview");
            jCheckBoxAutoPreview.setText("Auto Preview");
            jCheckBoxAutoPreview.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    actionEventAutoPreview(e);
                }
            });
            jCheckBoxAutoPreview.setActionCommand("autopreview");
        }
        return jCheckBoxAutoPreview;
    }

    /**
     * This method initializes jCheckBoxInvert
     *
     * @return javax.swing.JCheckBox
     */
    private JCheckBox getJCheckBoxInvert() {
        if (jCheckBoxInvert == null) {
            jCheckBoxInvert = new JCheckBox();
            //jCheckBoxInvert.setName("Auto Preview");
            jCheckBoxInvert.setText("Invert Output Image");
            jCheckBoxInvert.setSelected(false);
            jCheckBoxInvert.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    actionEventInvert(e);
                }
            });
            jCheckBoxInvert.setActionCommand("invert");
        }
        return jCheckBoxInvert;
    }

    /**
     * This method initializes jButtonPreview
     *
     * @return javax.swing.JCheckBox
     */
    private JButton getJButtonPreview() {
        if (jButtonPreview == null) {
            jButtonPreview = new JButton();
            jButtonPreview.setText("Preview");
            jButtonPreview.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    actionEventButtonPreview(e);
                }
            });
            jButtonPreview.setActionCommand("preview");
        }
        return jButtonPreview;
    }

    /**
     * This method initializes jButtonReset
     *
     * @return javax.swing.JCheckBox
     */
    private JButton getJButtonReset() {
        if (jButtonReset == null) {
            jButtonReset = new JButton();
            jButtonReset.setText("Reset");
//			jButtonReset.addActionListener(new ActionListener(){
//			public void actionPerformed(ActionEvent e){ actionEventButtonReset(e);}});
            jButtonReset.addActionListener((ActionListener) this);	//wird in der abgeleiteten Klasse behandelt
            jButtonReset.setActionCommand("reset");
        }
        return jButtonReset;
    }

    /**
     * This method initializes jButtonReset
     *
     * @return javax.swing.JCheckBox
     */
    private JButton getJButtonQuit() {
        if (jButtonQuit == null) {
            jButtonQuit = new JButton();
            jButtonQuit.setText("Quit");
            jButtonQuit.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    actionEventButtonQuit(e);
                }
            });
            jButtonQuit.setActionCommand("quit");
        }
        return jButtonQuit;
    }
    //----------------------------------------------------------------------------------------

    /**
     * This method gets the status of the Auto Preview Check Box
     * @return boolean selected or not selected
     */
    public boolean isAutoPreviewSelected() {
        return jCheckBoxAutoPreview.isSelected();
    }
    //----------------------------------------------------------------------------------------

    /**
     * This method gets the status of the Auto Preview Check Box
     * @return boolean selected or not selected
     */
    public boolean isInvertSelected() {
        return jCheckBoxInvert.isSelected();
    }
    //----------------------------------------------------------------------------------------

    /**
     * This method runs a preview
     */
    public void showPreview() {
        Manager.resetTogglePreviewIfRunning();
        Manager.setPreviewImage(null);
        String currFunc = IqmTools.getCurrImgProcessFunc();
        int i = IqmTools.getCurrManagerImgIndex();
        PlanarImage pi1 = null;

        //these functions don't need an image at all
        if (currFunc.equals("IqmFracIFS") || currFunc.equals("IqmCreateFracSurf")) {
            //do nothing
        } else {//most functions that need at least one image
            pi1 = Tank.getCurrentTankImageAt(i);
        }
        PlanarImage pi2 = null;
        if (currFunc.equals("IqmBUnwarpJ") || currFunc.equals("IqmTurboReg") || currFunc.equals("IqmCoGReg") || currFunc.equals("IqmImgStabilizer")) { //functions that need image pairs in a single stack
            pi2 = Tank.getCurrentTankImageAt(i + 1);
            if (pi2 == null) {
                return;
            }
        }
        if (currFunc.equals("IqmCalcImage") || currFunc.equals("IqmTempMatch")) {//functions that need two images (stacks)
            if (IqmTools.getCurrTankIndex() == Manager.getTankIndexMain()) {
                pi2 = Tank.getTankImageAt(Manager.getTankIndexMain2(), i);
            }
            if (IqmTools.getCurrTankIndex() == Manager.getTankIndexMain2()) {
                pi2 = Tank.getTankImageAt(Manager.getTankIndexMain(), i);
            }
        }
        Object ob = Manager.processImage(currFunc, pi1, pi2);
        if (ob instanceof PlanarImage) {
            Look.setNewImage((PlanarImage) ob);
            Manager.setPreviewImage((PlanarImage) ob);
        }
        if (ob instanceof JTable) {
            IqmTools.displayTableData((JTable) ob);
        }
        Manager.startTogglePreviewIfSelected();
    }

    /**
     * This method resets this frame;
     *
     */
    public void reset() {
        Manager.resetTogglePreviewIfRunning();
        Manager.setPreviewImage(null);
        int i = IqmTools.getCurrManagerImgIndex();
        PlanarImage pi = null;
        pi = Tank.getCurrentTankImageAt(i);
        Look.setNewImage(pi);
        //Manager.setPreviewImage((PlanarImage)ob);
        //set preferences to default
        this.getChooseParameterBlock().getModel().setSelectedItem("default");
        this.lastChoosenTemplateIndex = getChooseParameterBlock().getSelectedIndex();
    }

    //----------------------------------------------------------------------------------------
    /**
     * This method handles an action event
     */
    protected void actionEventAutoPreview(ActionEvent e) {
        if (this.isAutoPreviewSelected()) {
            this.showPreview();
        }
    }

    /**
     * This method handles an action event
     */
    protected void actionEventInvert(ActionEvent e) {
        if (this.isAutoPreviewSelected()) {
            this.showPreview();
        }
        //this.showPreview();
    }

    /**
     * This method handles an action event
     */
    protected void actionEventButtonPreview(ActionEvent e) {
        this.showPreview();
    }

    /**
     * This method handles an action event
     */
    protected void actionEventButtonReset(ActionEvent e) {
        System.out.println("IqmOperatorJFrame: Reset Command");
        //String currFunc = ("iqm"+this.getTitle()).toLowerCase();
//		String currFunc = IqmTools.getCurrImgProcessFunc();
//		ParameterBlockJAI pb = new ParameterBlockJAI(currFunc.toLowerCase());
//		IqmTools.setCurrParameterBlockJAI(pb);
//		//setParameterValues();
//		Class MyClass = null;
//		Method meth   = null;
//		try {
//			//MyClass = Class.forName("opgui."+currFunc+GUI);  //eventuell
//			MyClass = Class.forName("opgui."+currFunc+"GUI", true, Thread.currentThread().getContextClassLoader());
//			meth = MyClass.getMethod("setParameterBlock", ParameterBlockJAI.class);
//			meth.invoke(null, pb);
//			meth = MyClass.getMethod("setParameterValues");
//			meth.invoke(null);
//		} catch (Exception ee) {
//			ee.printStackTrace();
//		}
//		if(this.isAutoPreviewSelected()) this.showPreview();
    }

    /**
     * This method handles an action event
     */
    protected void actionEventButtonQuit(ActionEvent e) {

        IqmOperator.closeCurrIqmOperator();  //Umweg, da noch einiges gesetzt werden muss
    }
    //----------------------------------------------------------------------------------------

    @Override
    public void windowActivated(WindowEvent e) {
    }

    @Override
    public void windowClosed(WindowEvent e) {
    }

    @Override
    public void windowClosing(WindowEvent e) {
        IqmOperator.closeCurrIqmOperator();
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
    }

    @Override
    public void windowIconified(WindowEvent e) {
    }

    @Override
    public void windowOpened(WindowEvent e) {
    }
}//END

